package com.myf.typeHandlers;

import java.sql.*;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

@MappedTypes(ZonedDateTime.class)
@MappedJdbcTypes(JdbcType.TIMESTAMP_WITH_TIMEZONE)
public class ZonedDateTimeTypeHandler extends BaseTypeHandler<ZonedDateTime> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,
                                    ZonedDateTime parameter, JdbcType jdbcType) throws SQLException {
        ps.setObject(i, parameter.toOffsetDateTime(), Types.TIMESTAMP_WITH_TIMEZONE);
    }

    @Override
    public ZonedDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
        OffsetDateTime odt = rs.getObject(columnName, OffsetDateTime.class);
        return odt != null ? odt.toZonedDateTime() : null;
    }

    @Override
    public ZonedDateTime getNullableResult(ResultSet resultSet, int i) throws SQLException {
        OffsetDateTime odt = resultSet.getObject(i, OffsetDateTime.class);
        return odt != null ? odt.toZonedDateTime() : null;
    }

    @Override
    public ZonedDateTime getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        OffsetDateTime odt = callableStatement.getObject(i, OffsetDateTime.class);
        return odt != null ? odt.toZonedDateTime() : null;
    }

    // 其他必要方法...
}